/*
 * JavaSEFont.java
 *
 * Created on 2007-10-24 +0900
 *
 * $Revision: 45 $
 * $Date: 2008-10-26 17:16:20 +0000 (Sun, 26 Oct 2008) $
 */

package org.gikolet.framework.platform.javase.internal;

import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;

import org.gikolet.framework.ui.AbstractFont;
import org.gikolet.framework.ui.Font;

/**
 * @author Tetsutaro (latest modification by $Author: tetsutaro.ss $)
 * @version $Revision: 45 $ $Date: 2008-10-26 17:16:20 +0000 (Sun, 26 Oct 2008) $
 */
public class JavaSEFont extends AbstractFont {
    private java.awt.Font _font;

    private static FontRenderContext fontRenderContext = new FontRenderContext(null,
	    true, true);
    private LineMetrics lineMetrics;

    public final static int SIZE_LARGE = 30;
    public final static int SIZE_MEDIUM = 13;
    public final static int SIZE_SMALL = 10;
    public final static int SIZE_TINY = 8;

    JavaSEFont(JavaSEFontProvider provider, int face, int style, int size) {
	super(provider, face, style, size);

	String awtFontName = null;
	int awtStyle = 0;
	int awtSize = 0;

	switch (face) {
	    case Font.FACE_MONOSPACE:
		awtFontName = java.awt.Font.MONOSPACED;
		break;
	    case Font.FACE_PROPORTIONAL:
		awtFontName = java.awt.Font.SANS_SERIF;
		break;
	    case Font.FACE_SYSTEM:
		awtFontName = java.awt.Font.DIALOG;
		break;
	}

	if ((style & Font.STYLE_PLAIN) != 0) {
	    awtStyle |= java.awt.Font.PLAIN;
	} else {
	    if ((style & Font.STYLE_BOLD) != 0) {
		awtStyle |= java.awt.Font.BOLD;
	    }
	    if ((style & Font.STYLE_ITALIC) != 0) {
		awtStyle |= java.awt.Font.ITALIC;
	    }
	}
	if ((style & Font.STYLE_UNDERLINED) != 0) {
	}

	switch (size) {
	    case Font.SIZE_LARGE:
		awtSize = SIZE_LARGE;
		break;
	    case Font.SIZE_MEDIUM:
		awtSize = SIZE_MEDIUM;
		break;
	    case Font.SIZE_SMALL:
	    case Font.SIZE_TINY:
		awtSize = SIZE_SMALL;
		break;
	}

	this._font = new java.awt.Font(awtFontName, awtStyle, awtSize);

	this.lineMetrics = this._font.getLineMetrics("aA1ａＡあ", fontRenderContext);
    }

    public java.awt.Font getAWTFont() {
	return this._font;
    }

    public int getBaselinePosition() {
	return (int) this.lineMetrics.getAscent();
    }

    public int getHeight() {
	return (int) this.lineMetrics.getHeight();
    }

    public int stringWidth(String str) {
	if (str.length() == 0) {
	    return 0;
	}
	return (int) this._font.getStringBounds(str, fontRenderContext).getWidth();
    }

    public int charWidth(char c) {
	return (int) this._font.getStringBounds(String.valueOf(c), fontRenderContext)
		.getWidth();
    }

    public int substringWidth(String str, int offset, int lenght) {
	return (int) this._font.getStringBounds(str.substring(offset, offset + lenght),
		fontRenderContext).getWidth();

    }
}
